package com.hy.mydesktop.client.rpc.reader;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.DataReader;
import com.extjs.gxt.ui.client.data.ListLoadResult;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.hy.mydesktop.client.component.meta.GridPagingLoadResultModel;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceResult;
import com.hy.mydesktop.shared.rpc.meta.GxtToSeamServiceTreeNode;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-10-9；时间：下午05:33:08</li>
 * <li>类型名称：GxtToSeamServiceResultReader</li>
 * <li>设计目的：用于解析GxtToSeamServiceResult的DataReader</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class GxtToSeamServiceResultPagingReader<D> implements DataReader<D> {
	//private static GxtToSeamServiceResultListReader<GxtToSeamServiceResult> listreader = new GxtToSeamServiceResultListReader<GxtToSeamServiceResult>();
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：把GridPagingLoadResultModel（自定义数据类型），解析并转换成分页Grid的结果集PagingLoadResult</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-10-29；时间：下午下午03:05:09</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param loadConfig
	 * @param data
	 * @return
	 */
	@Override
	public D read(Object loadConfig, Object data) {
		//data是Rpc调用后的返回值result
		MyLoggerUtil.printError(GxtToSeamServiceResultPagingReader.class,"####GxtToSeamServiceResultPagingReader.read() is running...........................");

		System.out.println("GxtToSeamServiceResultReader.read().loadConfig is ######"+loadConfig);
		if (data instanceof GxtToSeamServiceResult) {
			GxtToSeamServiceResult gxtToSeamServiceResult =(GxtToSeamServiceResult)data;
			ModelData modelData = gxtToSeamServiceResult.getResult();
			System.out.println("gxtToSeamServiceResult.getResult()========modelData.get(listOfModelData)");				
			/*PagingLoadResult<ModelData> pagingLoadResult = new BasePagingLoadResult<ModelData>(
					(List<ModelData>)modelData.get("listOfModelData")//((GridPagingLoadResultModel)subModelData).getDataList(),
					//((GridPagingLoadResultModel)subModelData).getOffset(),
					//((GridPagingLoadResultModel)subModelData).getTotalLength()
					
				);
			pagingLoadResult.setOffset(((PagingLoadConfig)loadConfig).getOffset());
			//pagingLoadResult.setTotalLength(19);
			pagingLoadResult.setTotalLength(((PagingLoadConfig)loadConfig).getLimit());
			
			MyLoggerUtil.printError(GxtToSeamServiceResultPagingReader.class,"####GxtToSeamServiceResultPagingReader.read() is over...........................");
			return (D)pagingLoadResult;*/
			GridPagingLoadResultModel gridPagingLoadResultModel=(GridPagingLoadResultModel)modelData.get("listOfModelData");
			PagingLoadResult<ModelData> pagingLoadResult = new BasePagingLoadResult<ModelData>(
					gridPagingLoadResultModel.getDataList()//((GridPagingLoadResultModel)subModelData).getDataList(),
					//((GridPagingLoadResultModel)subModelData).getOffset(),
					//((GridPagingLoadResultModel)subModelData).getTotalLength()
				);
			pagingLoadResult.setOffset(((PagingLoadConfig)loadConfig).getOffset());
			//pagingLoadResult.setTotalLength(19);
			pagingLoadResult.setTotalLength(gridPagingLoadResultModel.getTotalLength());
			return (D) pagingLoadResult;
		}else {
			throw new RuntimeException("xxxxxxxxxxxxxxxxxxx");
		} 
		//assert false : "Error converting data";
		//return null;
	}
	
}
